const app = getApp();
const pageSize = 20;
Page({

  data: {
    total: 0,
    list: [],
    nomore: false,
    loadmore: false,
    empty: false,
    conditions: {
      card_sn: '',
      start_time: '',
      end_time: ''
    },
    isFilter: false,
    drawer: false,
  },

  onLoad: function () {
    this.page = 1;
    this.getRecycleList();
  },

  reloadData() {
    this.page = 1;
    this.setData({
      list: [],
      nomore: false,
      loadmore: false
    })
    this.getRecycleList();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      ++this.page;
      this.getRecycleList();
    }
  },

  getRecycleList() {
    app.ajax({
      url: "Partner/WaterCard/recoveryRecord",
      data: {
        card_sn: this.data.conditions.card_sn || '',
        start_time: this.data.conditions.start_time || '',
        end_time: this.data.conditions.end_time || '',
        page: this.page,
        pageSize: pageSize,
      },
      success: res => {
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          let source = res.data.data;
          let list = this.data.list.concat(source.list);
          this.setData({
            drawer: false,
            list,
            empty: false,
            total: source.count,
            loadmore: list.length < source.count ? true : false,
            nomore: list.length >= source.count ? true : false
          })
        } else {
          if (this.page == 1) {
            this.setData({
              drawer: false,
              nomore: false,
              loadmore: false,
              empty: true
            })
          } else {
            this.setData({
              drawer: false,
              nomore: true,
              loadmore: false,
            })
          }
        }
      },
      error: e => {
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })

  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },



  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  addRecycle() {
    let _self = this;
    wx.navigateTo({
      url: 'recycle',
      events: {
        updataRecycleData: function () {
          _self.reloadData();
        }
      }
    })
  }
})